<template>
  <!-- 订单添加修改页面 -->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增订单' : '编辑订单'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="orderStatus" label="订单状态">
        <el-select v-model="dataForm.orderStatus" :disabled="dataForm.orderStatus===7||dataForm.orderStatus===8?false:true" placeholder="请选择">
          <el-option v-for="item in optionsList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="receiveName" label="收货人">
        <el-input v-model="dataForm.receiveName" placeholder="请输入收货人的姓名" clearable></el-input>
      </el-form-item>
      <el-form-item prop="receivePhone" label="手机号码">
        <el-input v-model="dataForm.receivePhone" placeholder="请输入收货人的手机号码" clearable></el-input>
      </el-form-item>
<!--      <el-form-item prop="receiveProvince" label="所在省份">-->
<!--        <el-input v-model="dataForm.receiveProvince" placeholder="请输入收货地址所在的省份" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveCity" label="所在城市">-->
<!--        <el-input v-model="dataForm.receiveCity" placeholder="请输入收货地址所在的城市" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveCounty" label="所在区县">-->
<!--        <el-input v-model="dataForm.receiveCounty" placeholder="请输入收货地址所在的区县" clearable></el-input>-->
<!--      </el-form-item>-->
            <el-form-item label="区域">
              <el-cascader
                  v-model="value"
                  :options="options"
                  :props="{value:'name',label:'name',children:'children'}"
                  filterable></el-cascader>
            </el-form-item>
      <el-form-item prop="receiveAddress" label="详细地址">
        <el-input v-model="dataForm.receiveAddress" placeholder="请输入详细的街道地址，如门牌号、楼层等" clearable></el-input>
      </el-form-item>
<!--      <el-form-item prop="remark" label="订单备注">-->
<!--        <el-input v-model="dataForm.remark" placeholder="请输入订单备注" clearable></el-input>-->
<!--      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import addressData from "@/utils/address-data";
import {getDictDataList} from "@/utils";

export default {
  data() {
    return {
      visible: false,
      url: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imagelist:{},
      optionsList: [],
      options:addressData,              //地址列表
      value: [],                        //选中地址
      dataForm: {
        id: '',                         //订单id
        userId:'',                      //用户id
        createDateStart: '',            //开始创建时间
        createDateEnd: '',              //结束创建时间
        deliveryDateStart:'',           //开始发货时间
        deliveryDateEnd: '',            //结束发货时间
        deliveryNumber: '',             //配送单号
        dtlId: '',                      //订单明细
        orderStatus: '',                //订单的当前状态，如待付款、已支付、已发货、已完成等
        payDate: '',                    //付款时间
        payDateStart: '',               //开始付款时间
        payDateEnd: '',                 //结束付款时间
        receiveName: '',                //记录收货人的姓名。
        receivePhone: '',               //记录收货人的手机号码
        receiveProvince: '',            //记录收货地址所在的省、州或自治区
        receiveCity: '',                //记录收货地址所在的城市
        receiveCounty: '',              //记录收货地址所在的区县
        receiveAddress:'',              //记录详细的街道地址，如门牌号、楼层等
        remark: '',                     //用户对订单的附加说明或备注信息
        totalAmount: '',                //订单的总金额，等于商品价格乘以购买数量
        totalPoints: ''                 //平台积分
      }
    }
  },
  computed: {
    dataRule() {
      return {
        orderStatus: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receivePhone: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'},
        ],
        // receiveProvince: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // receiveCity: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // receiveCounty: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        receiveAddress: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        // remark: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.value = []
        this.optionsList = getDictDataList('order_status').map(item =>{
          return {
            dictLabel: item.dictLabel,
            dictValue: Number(item.dictValue),
            disabled: item.dictLabel=== '设计中'||item.dictLabel=== '生产中'||item.dictLabel=== '待发货'?false:true
          }})
        console.log(getDictDataList('order_status'))
        console.log(this.optionsList)
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userOrder/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.value = []
        if (res.data.receiveProvince){
          this.value.push(res.data.receiveProvince,res.data.receiveCity,res.data.receiveCounty)
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.value.length!==0){
              // receiveProvince: '',            //记录收货地址所在的省、州或自治区
              // receiveCity: '',                //记录收货地址所在的城市
              // receiveCounty: '',              //记录收货地址所在的区县
          this.dataForm['receiveProvince'] =this.value[0]
          this.dataForm['receiveCity'] =this.value[1]
          this.dataForm['receiveCounty'] =this.value[2]
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/userOrder', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
</style>
